package com.mikepenz.presenter.imp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps2d.model.LatLng;
import com.android.volley.VolleyError;
import com.mikepenz.app.R;
import com.mikepenz.app.activity.ParkActivity;
import com.mikepenz.bean.ParkBean;
import com.mikepenz.interactor.MainFragInteractor;
import com.mikepenz.interactor.imp.MainFragInteractorImp;
import com.mikepenz.presenter.ParkListPresenter;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.view.ParkListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 租用组列表
 * Created by Administrator on 2015/9/15.
 */
public class ParkListPresenterImp implements ParkListPresenter,RequestListener<List<ParkBean>> {

    MainFragInteractor interactor;
    ParkListView view;
    Context c;
    List<ParkBean> result ;

    int currentPage = 1;
    public ParkListPresenterImp (Context c){
        this(c,(ParkListView)c);
    }

    public ParkListPresenterImp(Context c,ParkListView view){
        this.c =c;
        this.view =view;
        interactor = new MainFragInteractorImp(c,this);
    }


    @Override
    public void getParking(LatLng latlng) {
        view.showLoad();
        String userId = CookicUntil.getUser().getUserId();
        interactor.getParking(latlng,userId,currentPage);
        currentPage ++;
    }

    @Override
    public void refreshParking(LatLng latlng) {
        view.showLoad();
        currentPage =1;
        String userId = CookicUntil.getUser().getUserId();
        interactor.getParking(latlng,userId,currentPage);

    }

    @Override
    public boolean checkCard() {
        return false;
    }

    @Override
    public boolean isLogin() {
        return false;
    }

    @Override
    public void Success(String tag, List<ParkBean> result) {
        List<Map<String,Object>> datas = new ArrayList<>();
        for(ParkBean p : result){
            Map<String,Object> data = (Map<String, Object>) JSON.parse(JSON.toJSON(p).toString());
            datas.add(data);
        }
        view.dismissLoad();
        view.refreshParking(datas , currentPage == 1);
        view.notifyDataSetChanged();
        if(this.result == null)
            this.result = result;
        else
            this.result.addAll(result);
    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoad();
        view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoad();
        view.showError(c.getString(R.string.system_error));
    }

    @Override
    public void parkOnClicked(int postion) {
        Intent intent = new Intent(c, ParkActivity.class);
        ParkBean bean = result.get(postion);
        Bundle bundle = new Bundle();
        bundle.putSerializable("park",bean);
        intent.putExtras(bundle);
        c.startActivity(intent);
    }
}
